﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYCableTrayConnection
{
	internal class HYCrossConnection
	{
		public HYCrossConnection(Document m_doc, HYCableTray<CableTray> ct1, HYCableTray<CableTray> ct2, HYCableTray<CableTray> ct3, HYCableTray<CableTray> ct4, VerticalCableTrayFittingTypeGroup vcf, XYZ intersectionPoint)
		{
			this.doc = m_doc;
			this.myCustomCableTray = ct1;
			this.myCustomCableTray1 = ct2;
			this.myCustomCableTray2 = ct3;
			this.myCustomCableTray3 = ct4;
			this.vcf = vcf;
			this.intersectionPoint = intersectionPoint;
			this.GetCableTrayConnectors(intersectionPoint);
		}

		private void GetCableTrayConnectors(XYZ intersectionPoint)
		{
			this.connector = this.myCustomCableTray.GetCloseConnector(intersectionPoint);
			this.connector1 = this.myCustomCableTray1.GetCloseConnector(intersectionPoint);
			this.connector2 = this.myCustomCableTray2.GetCloseConnector(intersectionPoint);
			this.connector3 = this.myCustomCableTray3.GetCloseConnector(intersectionPoint);
			if (this.connector == null || this.connector1 == null || this.connector2 == null || this.connector3 == null || this.connector.IsConnected || this.connector1.IsConnected || this.connector2.IsConnected || this.connector3.IsConnected)
			{
				throw new MyException("桥架已经连接");
			}
			this.otherConnector = Common.GetOtherConnector(this.myCustomCableTray, this.connector);
			this.otherConnector1 = Common.GetOtherConnector(this.myCustomCableTray1, this.connector1);
			this.otherConnector2 = Common.GetOtherConnector(this.myCustomCableTray2, this.connector2);
			this.otherConnector3 = Common.GetOtherConnector(this.myCustomCableTray3, this.connector3);
		}

		public bool NewDirectionVerticalCrossFitting()
		{
			this.verticalCableTrayFittingType = this.JudgeCrossFittingType();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewCrossFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateCrossFitting();
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray,
				this.myCustomCableTray1
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			double width = hycableTray2.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			double width2 = this.myCustomCableTray2.CableTray.Width;
			double height2 = this.myCustomCableTray2.CableTray.Height;
			this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(this.myCustomCableTray3.CableTray.Height);
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray);
			ElementId elementId = this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num2 = this.myCustomCableTray2.CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num;
			this.fitting.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num2);
			this.doc.Regenerate();
			this.TranslationCableTray(this.myCustomCableTray3.CableTray.Width);
			this.connector = this.myCustomCableTray.GetCloseConnector(this.intersectionPoint);
			this.connector1 = this.myCustomCableTray1.GetCloseConnector(this.intersectionPoint);
			this.CrossFittingConnect();
			Common.RefreshModel(this.doc, this.fitting);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width2);
			this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height2);
			this.doc.Regenerate();
			return true;
		}

		public bool NewVerticalCrossFitting()
		{
			this.verticalCableTrayFittingType = this.JudgeCrossFittingType();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewCrossFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateCrossFitting();
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray,
				this.myCustomCableTray1
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			HYCableTray<CableTray>[] trayWidthSort2 = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray2,
				this.myCustomCableTray3
			});
			HYCableTray<CableTray> hycableTray3 = trayWidthSort2[0];
			HYCableTray<CableTray> hycableTray4 = trayWidthSort2[1];
			double width = hycableTray2.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			double width2 = hycableTray4.CableTray.Width;
			double height2 = hycableTray4.CableTray.Height;
			hycableTray4.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray3.CableTray.Width);
			hycableTray4.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray3.CableTray.Height);
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray3, hycableTray);
			ElementId elementId = this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num2 = this.myCustomCableTray2.CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num;
			this.fitting.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num2);
			this.doc.Regenerate();
			this.TranslationCableTray(hycableTray3.CableTray.Width);
			this.connector = this.myCustomCableTray.GetCloseConnector(this.intersectionPoint);
			this.connector1 = this.myCustomCableTray1.GetCloseConnector(this.intersectionPoint);
			this.CrossFittingConnect();
			Common.RefreshModel(this.doc, this.fitting);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			hycableTray4.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width2);
			hycableTray4.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height2);
			this.doc.Regenerate();
			return true;
		}

		public bool NewUpDownVerticalCrossFitting()
		{
			this.verticalCableTrayFittingType = this.JudgeUpDownCrossFittingType();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewCrossFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateUpDownCrossFitting(this.verticalCableTrayFittingType);
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray2,
				this.myCustomCableTray3
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			double num = 0.0;
			double height = this.myCustomCableTray1.CableTray.Height;
			this.myCustomCableTray1.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			double width = hycableTray2.CableTray.Width;
			double height2 = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			if (this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownCrossA)
			{
				num = this.myCustomCableTray.CableTray.Width;
				this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(this.myCustomCableTray1.CableTray.Width);
			}
			else if (this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownCrossB || this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownCrossC)
			{
				num = this.myCustomCableTray.CableTray.Width;
				this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			}
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray);
			ElementId elementId = this.myCustomCableTray2.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num2 = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num3 = this.myCustomCableTray2.CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num2;
			this.fitting.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num3);
			this.doc.Regenerate();
			this.TranslationCableTray1(hycableTray.CableTray.Width);
			this.connector = this.myCustomCableTray.GetCloseConnector(this.intersectionPoint);
			this.CrossFittingConnect();
			Common.RefreshModel(this.doc, this.fitting);
			this.myCustomCableTray1.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height2);
			this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(num);
			return true;
		}

		private VerticalCableTrayFittingType JudgeUpDownCrossFittingType()
		{
			bool flag = this.otherConnector.Origin.Z > this.connector1.Origin.Z;
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			Transform coordinateSystem = this.connector.CoordinateSystem;
			XYZ xyz = (this.otherConnector1.Origin - this.connector1.Origin).Normalize();
			if (coordinateSystem.BasisY.IsAlmostEqualTo(xyz, 0.01) || coordinateSystem.BasisY.IsAlmostEqualTo(-xyz, 0.01))
			{
				if (flag)
				{
					result = VerticalCableTrayFittingType.VerticalDownCrossA;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpCrossA;
				}
			}
			else
			{
				double num = xyz.AngleOnPlaneTo(-coordinateSystem.BasisY, XYZ.BasisZ);
				if (Math.Abs(4.71238898038469 - num) < 0.001)
				{
					if (flag)
					{
						result = VerticalCableTrayFittingType.VerticalDownCrossB;
					}
					else
					{
						result = VerticalCableTrayFittingType.VerticalUpCrossB;
					}
				}
				else if (Math.Abs(Math.PI*.5 - num) < 0.001)
				{
					if (flag)
					{
						result = VerticalCableTrayFittingType.VerticalDownCrossC;
					}
					else
					{
						result = VerticalCableTrayFittingType.VerticalUpCrossC;
					}
				}
			}
			return result;
		}

		private void CrossFittingConnect()
		{
			XYZ xyz = (this.otherConnector.Origin - this.connector.Origin).Normalize();
			XYZ xyz2 = (this.otherConnector1.Origin - this.connector1.Origin).Normalize();
			XYZ xyz3 = (this.otherConnector2.Origin - this.connector2.Origin).Normalize();
			XYZ xyz4 = (this.otherConnector3.Origin - this.connector3.Origin).Normalize();
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz, 0.01))
				{
					this.connector.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz2, 0.01))
				{
					this.connector1.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz3, 0.01))
				{
					this.connector2.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz4, 0.01))
				{
					this.connector3.ConnectTo(connector);
				}
			}
		}

		private void TranslationCableTray1(double range)
		{
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			if (this.otherConnector.IsConnected)
			{
				return;
			}
			XYZ xyz = new XYZ(connector.Origin.X, connector.Origin.Y, this.otherConnector.Origin.Z) - this.otherConnector.Origin;
			double length = xyz.GetLength();
			if (length <= 1E-05)
			{
				return;
			}
			if (length <= range)
			{
				this.myCustomCableTray.CableTray.Location.Move(xyz);
				this.myCustomCableTray = new HYCableTray<CableTray>(this.myCustomCableTray.CableTray);
				return;
			}
			throw new MyException("请检查桥架位置");
		}

		private void TranslationCableTray(double range)
		{
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			if (!this.otherConnector.IsConnected)
			{
				XYZ xyz = new XYZ(connector.Origin.X, connector.Origin.Y, this.otherConnector.Origin.Z) - this.otherConnector.Origin;
				double length = xyz.GetLength();
				if (length <= 1E-05)
				{
					return;
				}
				if (length > range)
				{
					throw new MyException("请检查桥架位置");
				}
				this.myCustomCableTray.CableTray.Location.Move(xyz);
				this.myCustomCableTray = new HYCableTray<CableTray>(this.myCustomCableTray.CableTray);
			}
			if (this.otherConnector1.IsConnected)
			{
				return;
			}
			XYZ xyz2 = new XYZ(connector.Origin.X, connector.Origin.Y, this.otherConnector1.Origin.Z) - this.otherConnector1.Origin;
			double length2 = xyz2.GetLength();
			if (length2 == 1E-05)
			{
				return;
			}
			if (length2 <= range)
			{
				this.myCustomCableTray1.CableTray.Location.Move(xyz2);
				this.myCustomCableTray1 = new HYCableTray<CableTray>(this.myCustomCableTray1.CableTray);
				return;
			}
			throw new MyException("请检查桥架位置");
		}

		private void SetFittingParams(VerticalCableTrayFittingType type, HYCableTray<CableTray> fittingTray)
		{
			if (type == VerticalCableTrayFittingType.VerticalLeftCross || type == VerticalCableTrayFittingType.VerticalRightCross)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = this.myCustomCableTray3.CableTray.Width;
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = this.myCustomCableTray3.CableTray.Height;
				fittingRelevantParamer.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTray.CableTray.Width;
				fittingRelevantParamer.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTray.CableTray.Height;
				return;
			}
			Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer2 = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
			fittingRelevantParamer2.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTray.CableTray.Width;
			fittingRelevantParamer2.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTray.CableTray.Height;
			fittingRelevantParamer2.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = this.myCustomCableTray1.CableTray.Width;
			fittingRelevantParamer2.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = this.myCustomCableTray.CableTray.Height;
		}

		private void SetFittingParams(VerticalCableTrayFittingType type, HYCableTray<CableTray> fittingTrayH, HYCableTray<CableTray> fittingTrayV)
		{
			if (type == VerticalCableTrayFittingType.VerticalCrossA || type == VerticalCableTrayFittingType.VerticalCrossB)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTrayH.CableTray.Width;
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTrayH.CableTray.Height;
				fittingRelevantParamer.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTrayV.CableTray.Width;
				fittingRelevantParamer.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTrayV.CableTray.Height;
			}
		}

		private VerticalCableTrayFittingType JudgeCrossFittingType()
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			if (VerticalCableTrayFittingTypeGroup.VerticalLeftRightCross == this.vcf)
			{
				XYZ xyz = (this.otherConnector2.Origin - this.connector2.Origin).Normalize();
				XYZ xyz2 = (this.otherConnector3.Origin - this.connector3.Origin).Normalize();
				double num = xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
				if (Math.Abs(Math.PI*.5 - num) < 0.001)
				{
					result = VerticalCableTrayFittingType.VerticalLeftCross;
				}
				else if (Math.Abs(4.71238898038469 - num) < 0.001)
				{
					result = VerticalCableTrayFittingType.VerticalRightCross;
				}
			}
			else if (VerticalCableTrayFittingTypeGroup.VerticalCross == this.vcf)
			{
				Transform coordinateSystem = this.connector.CoordinateSystem;
				Transform coordinateSystem2 = this.connector3.CoordinateSystem;
				if (coordinateSystem.BasisY.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisY.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
				{
					result = VerticalCableTrayFittingType.VerticalCrossA;
				}
				else if (coordinateSystem.BasisY.IsAlmostEqualTo(coordinateSystem2.BasisZ, 0.01) || coordinateSystem.BasisY.IsAlmostEqualTo(-coordinateSystem2.BasisZ, 0.01))
				{
					result = VerticalCableTrayFittingType.VerticalCrossB;
				}
			}
			return result;
		}

		private FamilyInstance NewCrossFitting(VerticalCableTrayFittingType type, FamilySymbol fittingType)
		{
			UnifiedModified.ActivateFamilySymbol(fittingType);
			FamilyInstance result = this.doc.Create.NewFamilyInstance(this.intersectionPoint, fittingType, 0);
			this.doc.Regenerate();
			return result;
		}

		private void RotateCrossFitting()
		{
			if (VerticalCableTrayFittingTypeGroup.VerticalLeftRightCross == this.vcf || VerticalCableTrayFittingTypeGroup.VerticalCross == this.vcf)
			{
				Connector connector = null;
				foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
				{
					if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01))
					{
						connector = connector2;
						break;
					}
				}
				XYZ basisY = connector.CoordinateSystem.BasisY;
				XYZ xyz = -this.connector.CoordinateSystem.BasisY;
				double num = basisY.AngleOnPlaneTo(xyz, XYZ.BasisZ);
				if (num != 0.0)
				{
					Line line = Line.CreateUnbound((this.fitting.Location as LocationPoint).Point, XYZ.BasisZ);
					this.fitting.Location.Rotate(line, num);
				}
			}
		}

		private void RotateUpDownCrossFitting(VerticalCableTrayFittingType vcf)
		{
			Connector connector = null;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(this.fitting);
			foreach (Connector connector2 in conduitFittingConnectors)
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					connector = connector2;
					break;
				}
			}
			Connector connector3 = null;
			foreach (Connector connector4 in conduitFittingConnectors)
			{
				if (VerticalCableTrayFittingType.VerticalDownCrossA == vcf || VerticalCableTrayFittingType.VerticalUpCrossA == vcf)
				{
					if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisY, 0.01) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisY, 0.01))
					{
						connector3 = connector4;
						break;
					}
				}
				else if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisX, 0.01) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisX, 0.01))
				{
					connector3 = connector4;
					break;
				}
			}
			double num = (this.otherConnector1.Origin - this.connector1.Origin).Normalize().AngleOnPlaneTo(connector3.CoordinateSystem.BasisZ, XYZ.BasisZ);
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound((this.fitting.Location as LocationPoint).Point, XYZ.BasisZ);
				this.fitting.Location.Rotate(line, num);
			}
			this.doc.Regenerate();
		}

		private Document doc;

		private HYCableTray<CableTray> myCustomCableTray;

		private HYCableTray<CableTray> myCustomCableTray1;

		private HYCableTray<CableTray> myCustomCableTray2;

		private HYCableTray<CableTray> myCustomCableTray3;

		private XYZ intersectionPoint;

		private VerticalCableTrayFittingTypeGroup vcf;

		private VerticalCableTrayFittingType verticalCableTrayFittingType = VerticalCableTrayFittingType.Undefined;

		private Connector connector;

		private Connector connector1;

		private Connector connector2;

		private Connector connector3;

		private Connector otherConnector;

		private Connector otherConnector1;

		private Connector otherConnector2;

		private Connector otherConnector3;

		private FamilyInstance fitting;
	}
}
